package org.example.practiceexamples.service;

import lombok.RequiredArgsConstructor;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.repository.UserRepository;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class InitService {
    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    @EventListener(ApplicationReadyEvent.class)
    public void init() {
        long count = userRepository.count();
        if (count == 0) {
            User u = User.builder()
                    .account("admin")
                    .password(passwordEncoder.encode("admin"))
                    .description("一位非常好的老师")
                    .name("admin")
                    .phone("18837363069")
                    .status(1)
                    .profession("admin")
                    .role(User.ADMIN)
                    .build();
            userRepository.save(u);
        }
    }
}
